package cn.ltq.springbootplus.service;

import cn.ltq.springbootplus.dao.BookDao;
import cn.ltq.springbootplus.entity.Book;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl extends ServiceImpl<BookDao, Book> implements IBookService {

    @Autowired
    BookDao bookDao;

    @Override
    public List<Book> searchBooks(Book book) {
//        QueryWrapper queryWrapper = new QueryWrapper();
//        if (book.getAuthor() != null && book.getAuthor() != "") {
//            queryWrapper.like("author", book.getAuthor());
//        }
//        if (book.getName() != null && book.getName() != "") {
//            queryWrapper.like("name", book.getName());
//        }
//        if (book.getCategory() != null && book.getCategory() != "") {
//            queryWrapper.eq("category", book.getCategory());
//        }
//
//       return bookDao.selectList(queryWrapper);

        LambdaQueryWrapper<Book> lambdaQueryWrapper=new LambdaQueryWrapper();
        if (book.getAuthor() != null && book.getAuthor() != "") {
            lambdaQueryWrapper.like(Book::getAuthor,book.getAuthor());
        }
        if (book.getName() != null && book.getName() != "") {
            lambdaQueryWrapper.like(Book::getName,book.getName());
        }
        if (book.getCategory() != null && book.getCategory() != "") {
            lambdaQueryWrapper.eq(Book::getName, book.getCategory());
        }
        return bookDao.selectList(lambdaQueryWrapper);
    }

    @Override
    public IPage getPage(Integer pageNum) {
        IPage page = new Page(pageNum, 3);
        return bookDao.selectPage(page, null);

    }




}
